//
//  VCPayManger.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/1/27.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class VCPayManger: VCBase {
    private lazy var itemSwitchView:ItemSwitchView = {
        let ui = ItemSwitchView()
        ui.delegate = self
        let uiY = realHeight + MARGIN10
        ui.backgroundColor = UIColor.white
        ui.frame = CGRect(x: 0, y: uiY, width: SCREEN_WIDTH, height: 140)
        return ui
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        init_view()
        init_data()
    }
}
// MARK: 逻辑部分
extension VCPayManger: ItemSwitchViewDelegate {
    
    func clickGesture(ui: UISwitch) {
        let lock9View = Lock9ViewController()
        lock9View.callback = {password,status in
            if status != GestureStatus.SUCCESS {
                ui.isOn = false
                return
            }
            // 请求数据
            let dict = self.regStatusRequest(status: 1, desc: SP_GESTURE_PAY)
            // 请求
            self.doPost(api: "reg/status", dict: dict) { (dict) in
                ui.isOn = true
                self.show(text: "开通成功")
                // 存储密码
                UserDefaults.standard.setValue(password, forKey: SP_GESTURE_PAY_PWD)
            }
        }
        if ui.isOn {
            self.present(lock9View, animated: true, completion: nil)
        }else {
            // 关闭手势
            let dict = self.regStatusRequest(status: 0, desc: SP_GESTURE_PAY)
            // 请求
            self.doPost(api: "reg/status", dict: dict) { (dict) in
                ui.isOn = false
                self.show(text: "关闭成功")
                UserDefaults.standard.removeObject(forKey: SP_GESTURE_PAY_PWD)
            }
        }
    }
    
    func clickBiometric(ui: UISwitch) {
        if ui.isOn {
            // 打开指纹
            process { (isOK,type) in
                if isOK {
                    // 请求数据
                    let dict = self.regStatusRequest(status: 1, desc: (type == 1 ? SP_FINGER_PAY : SP_FACE_PAY))
                    // 请求
                    self.doPost(api: "reg/status", dict: dict) { (dict) in
                        self.show(text: "开通成功")
                        UserDefaults.standard.setValue(type, forKey: SP_BIOMETRIC_PAY_PWD)
                    }
                }else {
                    ui.isOn = false
                }
            }
        }else {
            // 关闭指纹
            let type = BiometricUtil.shareInstance.checkSupport()
            let dict = self.regStatusRequest(status: 0, desc: (type == 1 ? SP_FINGER_PAY : SP_FACE_PAY))
            // 请求
            self.doPost(api: "reg/status", dict: dict) { (dict) in
                self.show(text: "关闭成功")
                UserDefaults.standard.removeObject(forKey: SP_BIOMETRIC_PAY_PWD)
            }
        }
    }
}

// MARK: 初始化
extension VCPayManger {
    
    private func init_data() -> Void {
        let dict = NSMutableDictionary()
        dict.setValue(UIDevice.current.identifierForVendor?.uuidString, forKey: "deviceid")
        dict.setValue(UserDefaults.standard.value(forKey: SP_USERNAME) as? String, forKey: SP_USERNAME)
        // 请求
        self.doPost(api: "search/status", dict: dict) { (dict) in
            if self.isServerSuccess(dict: dict) != 1200 {
                return
            }
            // 解析数据
            let result = dict.value(forKey: SP_BODYREQUEST) as! NSDictionary
            // 设置开关状态
            let type = BiometricUtil.shareInstance.checkSupport()
            // 获取本地存储的开通状态
            let gesture = UserDefaults.standard.string(forKey: SP_GESTURE_PAY_PWD)
            let biometric = UserDefaults.standard.integer(forKey: SP_BIOMETRIC_PAY_PWD)
            // 设置开关状态
            if (result.value(forKey: SP_FACE_PAY) as? Int) == 1, type == 2, biometric == 2 {
                self.itemSwitchView.resetBiometricIsOn = true
            }
            if (result.value(forKey: SP_FINGER_PAY) as? Int) == 1, type == 1, biometric == 1{
                self.itemSwitchView.resetBiometricIsOn = true
            }
            if (result.value(forKey: SP_GESTURE_PAY) as? Int) == 1, gesture != nil {
                self.itemSwitchView.resetGestureIsOn = true
            }
        }
    }
    
    private func init_view() -> Void {
        view.addSubview(backroundWhite)
        view.addSubview(itemSwitchView)
    }
}
